﻿using System.Net.Http.Headers;
using System.Text;


namespace RBAC_Uthorization.Common
{
    public class HttpClientWrapper // 定义内部类HttpClientWrapper
    {
        private readonly HttpClient _httpClient; // 定义只读的HttpClient字段_httpClient

        /// <summary>
        /// Initializes a new instance of the <see cref="HttpClientWrapper"/> class with a bearer token.
        /// 初始化一个带有Bearer令牌的HttpClientWrapper类的新实例。
        /// </summary>
        /// <param name="token">The bearer token for authorization. 用于授权的Bearer令牌。</param>
        public HttpClientWrapper(string token) // 定义构造函数，接受一个字符串类型的token参数
        {
            _httpClient = new HttpClient(); // 初始化_httpClient字段
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); // 设置默认请求头的授权信息
        }

        /// <summary>
        /// Sends a GET request to the specified URL.
        /// 向指定的URL发送GET请求。
        /// </summary>
        /// <param name="url">The URL to send the GET request to. 发送GET请求的URL。</param>
        /// <returns>The response content as a string. 响应内容作为字符串返回。</returns>
        public async Task<string> GetAsync(string url) // 定义异步方法GetAsync，接受一个字符串类型的url参数
        {
            var response = await _httpClient.GetAsync(url); // 发送GET请求并等待响应
            response.EnsureSuccessStatusCode(); // 确保响应状态码表示成功
            return await response.Content.ReadAsStringAsync(); // 读取响应内容并作为字符串返回
        }

        /// <summary>
        /// Sends a POST request to the specified URL with the given content.
        /// 向指定的URL发送带有给定内容的POST请求。
        /// </summary>
        /// <param name="url">The URL to send the POST request to. 发送POST请求的URL。</param>
        /// <param name="content">The content to include in the POST request. POST请求中包含的内容。</param>
        /// <returns>The response content as a string. 响应内容作为字符串返回。</returns>
        public async Task<string> PostAsync(string url, string content) // 定义异步方法PostAsync，接受字符串类型的url和content参数
        {
            var httpContent = new StringContent(content, Encoding.UTF8, "application/json"); // 创建StringContent对象，指定内容、编码和媒体类型
            var response = await _httpClient.PostAsync(url, httpContent); // 发送POST请求并等待响应
            response.EnsureSuccessStatusCode(); // 确保响应状态码表示成功
            return await response.Content.ReadAsStringAsync(); // 读取响应内容并作为字符串返回
        }

        /// <summary>
        /// Sends a PUT request to the specified URL with the given content.
        /// 向指定的URL发送带有给定内容的PUT请求。
        /// </summary>
        /// <param name="url">The URL to send the PUT request to. 发送PUT请求的URL。</param>
        /// <param name="content">The content to include in the PUT request. PUT请求中包含的内容。</param>
        /// <returns>The response content as a string. 响应内容作为字符串返回。</returns>
        public async Task<string> PutAsync(string url, string content) // 定义异步方法PutAsync，接受字符串类型的url和content参数
        {
            var httpContent = new StringContent(content, Encoding.UTF8, "application/json"); // 创建StringContent对象，指定内容、编码和媒体类型
            var response = await _httpClient.PutAsync(url, httpContent); // 发送PUT请求并等待响应
            response.EnsureSuccessStatusCode(); // 确保响应状态码表示成功
            return await response.Content.ReadAsStringAsync(); // 读取响应内容并作为字符串返回
        }

        /// <summary>
        /// Sends a DELETE request to the specified URL.
        /// 向指定的URL发送DELETE请求。
        /// </summary>
        /// <param name="url">The URL to send the DELETE request to. 发送DELETE请求的URL。</param>
        /// <returns>The response content as a string. 响应内容作为字符串返回。</returns>
        public async Task<string> DeleteAsync(string url) // 定义异步方法DeleteAsync，接受一个字符串类型的url参数
        {
            var response = await _httpClient.DeleteAsync(url); // 发送DELETE请求并等待响应
            response.EnsureSuccessStatusCode(); // 确保响应状态码表示成功
            return await response.Content.ReadAsStringAsync(); // 读取响应内容并作为字符串返回
        }
    }
}
